// ==UserScript==
// @name         pixiv去广告
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  
// @author       RenjiYuusei
// @match        https://www.pixiv.net/*
// @match        https://touch.pixiv.net/*
// @icon         https://raw.gitcode.com/gagghik1/greyz4k/raw/main/pixiv/icon.png
// @license      GPL-3.0-only
// @grant        none
// @run-at       document-start
// @downloadURL  https://raw.gitcode.com/gagghik1/greyz4k/raw/main/pixiv/user.js
// @updateURL    https://raw.gitcode.com/gagghik1/greyz4k/raw/main/pixiv/meta.js
// ==/UserScript==

(() => {
    // List of advertising selectors to be blocked
    const adSelectors = [
        ".ads", ".ad-container", ".billboard", ".ads-top-info",
        '[class*="ad-"]', '[class*="ads-"]', '[id*="ad-"]', '[id*="ads-"]',
        ".premium-lead-t", ".premium-lead-new-t", ".promotional-popup",
        ".ad-footer", ".ad-header", ".ad-sidebar", ".billboard-ad",
        ".js-premium-lead-header", ".js-premium-lead-footer", ".spotlight-ads",
        ".ad-interstitial", 'iframe[src*="ads"]', "div[data-ad-slot]",
        ".adsbygoogle", "#footer-ads", "#headerAd", ".ads-article-bottom",
        ".ads-article-top", ".ads-sidebar", ".advertisement", ".pixiv-ad",
        ".premium-promotion", "[data-gtm-recommend-ad]", '[data-gtm-value*="ad_"]',
        // Mobile specific selectors
        ".sp-ads", ".mobile-ad", ".mobile-advertisement",
        '[class*="sp-ad-"]', '[id*="sp-ad-"]',
        ".mobile-premium-lead", ".mobile-promotional",
        'div[class*="mobile-ads"]', 'div[id*="mobile-ads"]',
        // Additional ad selectors
        ".sc-e9a5cd03-2", ".bGISCJ", "#adsdk--R1mhutb6",
        'div[id^="adsdk--"]', 'iframe[data-uid^="R"]'
    ];

    // Function to remove ads
    function removeAds() {
        // Remove elements matching the selectors
        adSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(element => {
                element.remove();
            });
        });

        // Remove ad close buttons and their containers
        document.querySelectorAll('img[src*="close_icon"][style*="position: absolute"], img[src*="close_button"]').forEach(img => {
            const container = img.closest('div');
            if (container) container.remove();
        });
    }

    // Add CSS to hide ads
    function addBlockingStyles() {
        const style = document.createElement('style');
        style.textContent = `
            ${adSelectors.join(', ')},
            img[src*="close_icon"][style*="position: absolute"],
            img[src*="close_button"] {
                display: none !important;
                visibility: hidden !important;
                opacity: 0 !important;
                pointer-events: none !important;
                width: 0 !important;
                height: 0 !important;
                position: absolute !important;
                top: -9999px !important;
                left: -9999px !important;
            }
            /* Mobile specific styles */
            body.touch-device .ad-container,
            body.touch-device [class*="sp-ad"],
            body.touch-device .mobile-ad {
                display: none !important;
            }
        `;
        document.head.appendChild(style);
    }

    // Observe DOM changes to remove dynamic ads
    function observeDOMChanges() {
        const observer = new MutationObserver((mutations) => {
            let hasNewNodes = mutations.some(mutation => mutation.addedNodes.length > 0);
            if (hasNewNodes) removeAds();
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // Initialize
    function init() {
        addBlockingStyles();
        removeAds();
        observeDOMChanges();
        
        // Process events to ensure ads are removed
        window.addEventListener('load', removeAds);
        document.addEventListener('DOMContentLoaded', removeAds);
        window.addEventListener('scroll', removeAds);
        // Handle touch events for mobile
        document.addEventListener('touchstart', removeAds, {passive: true});
        document.addEventListener('touchend', removeAds, {passive: true});
        setInterval(removeAds, 1000);
    }

    // Run script
    init();
})();
